package com.demo.controller;

import com.demo.entity.*;
import com.demo.entity.Class;
import com.demo.repository.ClassRepository;
import com.demo.repository.GradeRepository;
import com.demo.repository.StudentRepository;
import com.demo.repository.TeacherRepository;
import com.demo.service.AdminServer;
import com.demo.service.StudentService;
import com.demo.service.TeacherService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    AdminServer adminServer;
    @Autowired
    StudentRepository studentRepository;
    @Autowired
    TeacherRepository teacherRepository;
    @Autowired
    StudentService studentService;
    @Autowired
    TeacherService teacherService;
    @Autowired
    ClassRepository classRepository;
    @Autowired
    GradeRepository gradeRepository;
    //管理员登陆
    @RequestMapping("/adminLogin")
    @ResponseBody
    public List teacherLogin(HttpServletRequest request,@RequestBody User user) {

        if(user.getCode().equals("1")){
            List list=new ArrayList();
            if(studentService.Login(Integer.parseInt(user.getUsername()),user.getPassword()).equals("登录成功")){
                request.getSession().setAttribute("student",user.getUsername());
                list.add("/admin/student");
                list.add(user);
                return list;
            }
            return list;
        }else if(user.getCode().equals("2")){
            List list=new ArrayList();
            //request.getSession().setAttribute("teacher",);
            Teacher teacher = teacherService.Login(user.getUsername(), user.getPassword());
            request.getSession().setAttribute("teacher",teacher);
            if (teacher == null) {
                return list;
            } else {

                list.add("/admin/teacher");
                list.add(user);
                return list;

            }
        }else {
            List list=new ArrayList();
            if (user.getUsername().equals("admin")&&user.getPassword().equals("admin123")) {
                list.add("/admin/admin");
                list.add(user);
                return list;
            } else {
                return list;
            }
        }

    }
    @RequestMapping("/admin")
    public String adminlogin() {
        return "admin";
    }
    @RequestMapping("/teacher")
    public String teacherlogin() {
        return "home";
    }
    @RequestMapping("/student")
    public String studentlogin() {
        return "student";
    }



    //展示学生信息
    @RequestMapping("/showStudent")
    @ResponseBody
    public PageInfo<Student> showStudent(Integer classId,String stuName,Integer pageNo,Integer pageSize){
        PageHelper.startPage(pageNo, pageSize);
        List<Student> students = adminServer.selectStudent(classId, stuName);
        PageInfo<Student> pageInfo = new PageInfo<>(students);
        return pageInfo;
    }
    //插入学生信息
    @PostMapping("/insertStudent")
    @ResponseBody
    public Result insertStudent(@RequestBody Student student) {
        StudentExample studentExample=new StudentExample();
        studentExample.createCriteria().andStuIdEqualTo(student.getStuId());
        List<Student> students = studentRepository.selectByExample(studentExample);
        if(students.isEmpty()){
            adminServer.insertStudent(student);
            ClassExample classExample=new ClassExample();
            classExample.createCriteria().andClassnameEqualTo(student.getClassId());
            Class classes = classRepository.selectByExample(classExample).get(0);
            String subjectNames=classes.getSubjectName();
            String[] split = subjectNames.split(",");

            for(String subject:split){
                Grade grade=new Grade();
                grade.setStuId(student.getStuId());
                grade.setSubjectName(subject);
                grade.setSeason(classes.getSeason());
                grade.setClassId(student.getClassId());
                gradeRepository.insertSelective(grade);
            }
            return new Result(200,"添加成功");
        }else {
            return new Result(400,"学号已经存在");
        }

    }

    public static void main(String[] args) {
        String s="语文,数学";
        String[] split = s.split(",");
        for(String ss:split){
            System.out.println(ss);
        }
    }
    //删除学生信息
    @PostMapping("/deleteStudent")
    @ResponseBody
    public String deleteStudent(@RequestBody Student student){

        studentRepository.deleteByPrimaryKey(student.getId());
        return "删除成功";
    }
    //批量删除学生信息
    @PostMapping("/deleteStudents")
    @ResponseBody
    public String deleteStudent(@RequestBody DeleteId deleteId){
        for(Integer id:deleteId.getIds()){
            studentRepository.deleteByPrimaryKey(id);
        }
        return "删除成功";
    }
    //修改学生信息
    @PutMapping("/updateStudent")
    @ResponseBody
    public Result updateStudent(@RequestBody Student student){
        Student student1 = studentRepository.selectByPrimaryKey(student.getId());
        if(student1.getStuId().equals(student.getStuId())){
            StudentExample studentExample2=new StudentExample();
            studentExample2.createCriteria().andIdEqualTo(student.getId());
            studentRepository.updateByExampleSelective(student,studentExample2);
            return new Result(200,"修改成功");
        }else {
            StudentExample studentExample = new StudentExample();
            studentExample.createCriteria().andStuIdEqualTo(student.getStuId());
            List<Student> students = studentRepository.selectByExample(studentExample);
            if (students.isEmpty()) {
                StudentExample studentExample2 = new StudentExample();
                studentExample2.createCriteria().andIdEqualTo(student.getId());
                studentRepository.updateByExampleSelective(student, studentExample2);
                return  new Result(200,"修改成功");
            } else {
                return  new Result(400,"学号已经存在");
            }
        }
    }
    //查询老师信息
    @RequestMapping("/showTeacher")
    @ResponseBody
    public PageInfo<Teacher> showTeacher(String teacherName,Integer pageNo,Integer pageSize){
        PageHelper.startPage(pageNo, pageSize);
        List<Teacher> teachers = adminServer.selectTeacher(teacherName);
        PageInfo<Teacher> pageInfo = new PageInfo<>(teachers);
        return pageInfo;
    }
    //插入老师信息
    @PostMapping("/insertTeacher")
    @ResponseBody
    public Result insertTeacher(@RequestBody Teacher teacher) {

        TeacherExample teacherExample=new TeacherExample();
        teacherExample.createCriteria().andTeacherNameEqualTo(teacher.getTeacherName());
        List<Teacher> teachers = teacherRepository.selectByExample(teacherExample);
        if(teachers.isEmpty()){
            adminServer.insertTeacher(teacher);
            return new Result(200,"添加成功");
        }else {
            return new Result(400,"老师名字已经存在,如果重名的话请使用姓名+自定义编号");
        }
    }
    //删除老师信息
    @DeleteMapping("/deleteTeacher")
    @ResponseBody
    public String deleteTeacher(@RequestBody Teacher teacher){
        teacherRepository.deleteByPrimaryKey(teacher.getId());
        return "删除成功";
    }
    //批量删除老师信息
    @PostMapping("/deleteTeachers")
    @ResponseBody
    public String deleteTeachers(@RequestBody DeleteId deleteId){
        for(Integer id:deleteId.getIds()){
            teacherRepository.deleteByPrimaryKey(id);
        }
        return "删除成功";
    }
    //修改老师信息
    @PutMapping("/updateTeacher")
    @ResponseBody
    public Result updateTeacher(@RequestBody Teacher teacher) {
        Teacher teacher1 = teacherRepository.selectByPrimaryKey(teacher.getId());
        if (teacher.getTeacherName().equals(teacher1.getTeacherName())) {
            TeacherExample teacherExample1 = new TeacherExample();
            teacherExample1.createCriteria().andIdEqualTo(teacher.getId());
            teacherRepository.updateByExampleSelective(teacher, teacherExample1);
            return new Result(200, "修改成功");
        } else {
            TeacherExample teacherExample = new TeacherExample();
            teacherExample.createCriteria().andTeacherNameEqualTo(teacher.getTeacherName());
            List<Teacher> teachers = teacherRepository.selectByExample(teacherExample);
            if (teachers.isEmpty()) {
                TeacherExample teacherExample1 = new TeacherExample();
                teacherExample1.createCriteria().andIdEqualTo(teacher.getId());
                teacherRepository.updateByExampleSelective(teacher, teacherExample1);
                return new Result(200, "修改成功");
            } else {
                return new Result(400, "老师名字已经存在,如果重名的话请使用姓名+自定义编号");
            }
        }

    }
}
